package com.san.simulation.model.impl;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;

import com.san.simulation.model.api.Element;

public class ActivitiProcessElement implements Element {

	private ActivityImpl element;
	private PvmTransition transition;
	private int level;
	private boolean visited;

	public ActivitiProcessElement(ActivityImpl element) {
		super();
		this.element = element;
	}

	public ActivitiProcessElement(PvmTransition transition) {
		super();
		this.transition = transition;
	}

	public String getId() {
		if (this.element != null) {
			return element.getId();

		} else if (this.transition != null) {
			return this.transition.getId();
		}
		return null;
	}

	public List<Element> getOutgoingTransitions() {

		if (this.element.getOutgoingTransitions() != null) {
			List<Element> outgoingTransitionsWrapped = new ArrayList<Element>();
			for (PvmTransition transition : this.element
					.getOutgoingTransitions()) {
				outgoingTransitionsWrapped.add(new ActivitiProcessElement(
						transition));
			}
			return outgoingTransitionsWrapped;
		}

		return null;
	}

	public ActivityImpl getElement() {
		return element;
	}

	public void setElement(ActivityImpl element) {
		this.element = element;
	}

	public Object getProperty(String propertyName) {
		if (this.element != null) {
			return this.element.getProperty(propertyName);

		} else if (this.transition != null) {
			return this.transition.getProperty(propertyName);
		}
		return null;
	}

	public Element getSource() {
		
		
			return new ActivitiProcessElement((ActivityImpl) transition.getSource());
		
	}

	public String getType() {
		if (this.element != null) {
			return (String) element.getProperty("type");

		} else if (this.transition != null) {
			return "sequenceFlow";
		}
		return null;
	}

	public int hashCode() {
		return getId().hashCode();
	}

	@Override
	public boolean equals(Object obj) {

		if (obj instanceof Element) {

			if (this.getId().equalsIgnoreCase(((Element) obj).getId())) {
				return true;
			} else {
				return false;
			}
		} else {
			return super.equals(obj);
		}
	}

	public String toString() {

		if (this.element != null) {
			return element.toString();

		} else if (this.transition != null) {
			return this.transition.toString();
		}
		return null;
	}

	public String getName() {
		String name = "";
		if (this.element != null) {
			name = (String) element.getProperty("name");

		} else if (this.transition != null) {
			name = (String) this.transition.getProperty("name");
		}
		return name == null ? "" : name;
	}

	public void setLevel(int level) {
		this.level = level;

	}

	public int getLevel() {
		return this.level;
	}

	public int compareTo(Element o) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Element getDestination() {
		if (transition != null) {
			return new ActivitiProcessElement(
					(ActivityImpl) transition.getDestination());
		} else {
			return null;
		}
	}

	public boolean getVisited() {
		return this.visited;
	}

	public void setVisited(boolean visited) {
		this.visited = visited;
	}

	public PvmTransition getTransition() {
		return transition;
	}

	public void setTransition(PvmTransition transition) {
		this.transition = transition;
	}
	
	

}
